<script setup>
import { ref, onMounted, watch } from 'vue'
import { getPatientList, getPatientInfo } from '@/api/patient'
import { useStatusStore } from '../../../stores'

const patients = ref([])
const total = ref(0)
// 查询条件
const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(10)

onMounted(async () => {
  handleSearchClick()
})

const handleSearchClick = async () => {
  const res = await getPatientList({
    name: searchQuery.value,
    pageNumber: currentPage.value,
    pageSize: pageSize.value
  })

  patients.value = res.data.records
  total.value = res.data.total
}

const handlePageQuery = async () => {
  handleSearchClick()
}

const showPatientModal = ref(false)
const currentPatient = ref({})

const openPatientModal = async (patient) => {
  showPatientModal.value = true

  const res = await getPatientInfo(patient.patientId)

  currentPatient.value = res.data
}

const statusStore = useStatusStore()
watch(
  () => statusStore.componentKey,
  async () => {
    handleSearchClick()
  }
)
</script>

<template>
  <div class="patients-container">
    <header class="header">
      <h2>患者管理</h2>
      <input-search
        v-model="searchQuery"
        class="search-input"
        @search-click="handleSearchClick"
        text="搜索患者姓名"
      ></input-search>
    </header>

    <!-- 患者列表 -->
    <div class="patients-list">
      <overview-card
        v-for="patient in patients"
        :key="patient.id"
        class="patient-card"
      >
        <div class="patient-avatar">
          {{ patient.name.charAt(0) }}
        </div>
        <div class="patient-info">
          <div class="patient-name">
            {{ patient.name }}
            <span class="patient-id">ID: {{ patient.patientId }}</span>
          </div>
          <div class="patient-details">
            <span>{{ patient.gender }}</span>
            <!-- <span>{{ patient.age }}岁</span> -->
            <span>{{ patient.deptName }}</span>
            <span>{{ patient.location }}</span>
          </div>
        </div>
        <div class="patient-actions">
          <Button
            class="btn-edit"
            @click="openPatientModal(patient)"
            text="查看详情"
          >
          </Button>
        </div>
      </overview-card>
    </div>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 50, 100, 500]"
      :size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handlePageQuery"
      @current-change="handlePageQuery"
    />

    <!-- 患者modal -->
    <popup-card :show="showPatientModal" class="patient-modal">
      <div class="modal-content">
        <h2>患者详情</h2>
        <el-form>
          <el-form-item label="患者姓名">
            <el-input v-model="currentPatient.patientName" disabled />
          </el-form-item>
          <el-form-item label="患者性别">
            <el-input v-model="currentPatient.gender" disabled />
          </el-form-item>
          <el-form-item label="患者地址">
            <el-input v-model="currentPatient.address" disabled />
          </el-form-item>
          <el-form-item label="患者电话">
            <el-input v-model="currentPatient.phoneNumber" disabled />
          </el-form-item>
          <el-form-item label="入院时间">
            <el-date-picker
              v-model="currentPatient.admissionTime"
              type="date"
              disabled
              style="width: 100%"
            />
          </el-form-item>
          <el-form-item label="患者科室">
            <el-input v-model="currentPatient.deptName" disabled />
          </el-form-item>
          <el-form-item label="诊治医生">
            <el-input v-model="currentPatient.doctorName" disabled />
          </el-form-item>
          <el-form-item label="患者病房">
            <el-input v-model="currentPatient.wardId" disabled />
          </el-form-item>
          <el-form-item label="患者床位">
            <el-input v-model="currentPatient.bedId" disabled />
          </el-form-item>
          <el-form-item label="住院位置">
            <el-input v-model="currentPatient.location" disabled />
          </el-form-item>
          <div class="modal-actions">
            <Button
              class="btn-cancel"
              @click.prevent="showPatientModal = false"
              text="取消"
            >
            </Button>
          </div>
        </el-form>
      </div>
    </popup-card>
  </div>
</template>

<style lang="scss" scoped>
@use '@/assets/scss/abstracts/variables' as *;
@use '@/assets/scss/abstracts/mixins' as *;
@use 'sass:color';

.patients-container {
  padding: 2rem;

  .header {
    @include flexSpaceBetweenCenter;
    margin-bottom: 1.5rem;
    min-height: 5rem;
  }

  .patients-list {
    @include grid-35-2;
  }

  .el-pagination {
    margin-top: 2rem;
    justify-self: center;
  }

  .patient-card {
    @include flexSpaceBetweenCenter;
  }

  // 其他样式
  .patient-avatar {
    @include flexCenter;
    background-color: $color-orange-1;
    color: $color-white;
    border-radius: 50%;
    width: 5rem;
    height: 5rem;

    font-size: 2.4rem;
    margin-right: 1.5rem;
  }

  .patient-info {
    flex-grow: 1;

    .patient-name {
      font-size: 1.8rem;
      font-weight: bold;
      margin-bottom: 0.5rem;

      .patient-id {
        font-size: 1.4rem;
        color: $color-grey-1;
      }
    }

    .patient-details {
      font-size: 1.4rem;
      color: $color-grey-1;
      display: flex;
      gap: 1rem;
    }
  }

  .patient-actions {
    display: flex;
    flex-direction: column;

    .btn-edit {
      background-color: $color-primary-dark-2;
      padding: 0.7rem;
      border-radius: 0.7rem;

      &:hover {
        background-color: color.adjust($color-primary-dark-2, $lightness: -10%);
        transform: none;
      }

      &:focus {
        transform: none;
      }
    }
  }

  .modal-content {
    padding: 1rem;
    width: 40rem;
    max-height: 40rem;
    overflow: auto;

    h2 {
      margin-bottom: 2rem;
    }

    .el-input {
      width: 100%;
    }

    .modal-actions {
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .btn-cancel {
        width: 6rem;
        padding: 0.8rem;
        border-radius: 0.8rem;

        &:hover {
          background-color: color.adjust(
            $color-primary-dark-2,
            $lightness: -10%
          );
        }
      }
    }
  }
}
</style>
